<?php
class ModulePaymentFactory
{

    /**
     * @var ModulePayment
     */
    private static $instance;

    /**
     * Get ModulePayment implementation instance.
     */
    public static function get()
    {
        if (!self::$instance) {
            throw new SzException(30200, null, ModulePaymentConst::MODULE_NAME);
        }
        return self::$instance;
    }

    /**
     * Initialize the platform ModulePayment implementation instance in the factory,
     * and then use ModulePaymentFactory::get() to get the instance.
     * Param $userId is possible to be NULL, when this function called on index.php page,
     * there is only platform id, and no user id.
     *
     * @param int $userId player user id, not platform id
     * @param string $platform platform name, ModulePayment::$PF*
     * @throws SzException 30201
     * @return ModulePayment $instance
     */
    public static function init($userId, $platform)
    {
        $platformOn = SzConfig::get()->loadAppConfig('app', 'PLATFORM_ON');

        if ($platformOn) {
            // platform APIs enabled
            switch ($platform) {
                case ModulePaymentConst::PFFB:
                    self::$instance = new ModulePaymentFacebook($userId, ModulePaymentConst::PFFB);
                    break;
                case ModulePaymentConst::PFSA:
                    self::$instance = new ModulePaymentSina($userId, ModulePaymentConst::PFSA);
                    break;
                case ModulePaymentConst::PFVK:
                    self::$instance = new ModulePaymentVk($userId, ModulePaymentConst::PFVK);
                    break;
                case ModulePaymentConst::PFOGZ:
                    self::$instance = new ModulePaymentOgz($userId, ModulePaymentConst::PFOGZ);
                    break;
                case ModulePaymentConst::PFAG:
                    self::$instance = new ModulePaymentArmorgame($userId, ModulePaymentConst::PFAG);
                    break;
                case ModulePaymentConst::PFNO:
                    self::$instance = new ModulePaymentNo(); // do not initialize self instance, just return no platform specified instance
                    break;
                case ModulePaymentConst::PFSZ:
                    self::$instance = new ModulePaymentShinezone($userId, ModulePaymentConst::PFSZ); // do not initialize self instance, just return no platform specified instance
                    break;
                default:
                    throw new SzException(30502, $platform, ModulePaymentConst::MODULE_NAME); // platform name invalid
                    break;
            }
        } else {
            // platform APIs disabled
            self::$instance = new ModulePaymentNo();
        }
        return self::$instance;
    }
}